package com.yaozheng.vocationaltraining.fragment.exam;

import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.adapter.PKMorePersonListAdapter;
import com.yaozheng.vocationaltraining.adapter.PKResultListAdapter;
import com.yaozheng.vocationaltraining.bean.PKMorePersonBean;
import com.yaozheng.vocationaltraining.bean.PKResultBean;
import com.yaozheng.vocationaltraining.bean.PKResultPlayerBean;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.exam.IPKResultView;
import com.yaozheng.vocationaltraining.service.exam.PKMorePersonHallService;
import com.yaozheng.vocationaltraining.service.exam.PKResultService;
import com.yaozheng.vocationaltraining.service.impl.exam.PKMorePersonHallServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.exam.PKResultServiceImpl;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

/**
 * Created by moon on 2017/11/19.
 */
@EFragment(R.layout.activity_pk_result)
public class PKResultFragment extends BaseFragment
        implements IPKResultView {
    @ViewById
    ListView listView;
    PKResultListAdapter pkResultListAdapter;

    @ViewById
    TextView headTitleText;


    private BitmapLoader userBitmapLoader;
    public HomeActivity getHomeActivity() {
        return (HomeActivity) getActivity();
    }

    @Bean(PKResultServiceImpl.class)
    PKResultService pkResultService;

    public int getGameId() {
        return gameId;
    }

    public void setGameId(int gameId) {
        this.gameId = gameId;
    }

    int gameId;

    PKResultBean resultBean;

    @AfterViews
    public void initView() {
        headTitleText.setText("PK赛结果");
        pkResultService.init(this);
        userBitmapLoader = new BitmapLoader(Glide.with(this), R.drawable.user_img);


        resultBean = new PKResultBean();

        pkResultListAdapter = new PKResultListAdapter(this.getActivity()
                ,resultBean.getPlayers());
        listView.setAdapter(pkResultListAdapter);

        onRefresh();
    }

    private void onRefresh() {
        pkResultService.list(gameId);
    }

    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        try {
            cancelLoadDataProgressDialog();
            JSONObject resultObject = TypeUtils.getJsonObject(jsonObject,"data","game");
            resultBean.setId(resultObject.getInt("id"));
            resultBean.setPaperCateName(resultObject.getString("paperCateName"));
            resultBean.setGameRuleName(resultObject.getString("gameRuleName"));
            resultBean.setStartTime(resultObject.getString("startTime"));


            JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","game","players");
            if (dataList != null) {
                resultBean.getPlayers().clear();

                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject playerJsonObject = dataList.getJSONObject(i);
                    PKResultPlayerBean bean = new PKResultPlayerBean();
                    bean.setPlayerId(playerJsonObject.getInt("playerId"));
                    bean.setPlayerName(playerJsonObject.getString("playerName"));
                    bean.setPlayerAvatar(playerJsonObject.getString("playerAvatar"));
                    bean.setPlayerPosition(playerJsonObject.getString("playerPosition"));
                    bean.setPlayerStoreId(playerJsonObject.getInt("playerStoreId"));
                    bean.setScore(playerJsonObject.getInt("score"));
                    bean.setResult(playerJsonObject.getBoolean("result"));
                    bean.setStatus(playerJsonObject.getInt("status"));
                    bean.setPlayerStoreName(playerJsonObject.getString("playerStoreName"));

                    resultBean.getPlayers().add(bean);
                }

                pkResultListAdapter.setData(resultBean.getPlayers());
                pkResultListAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
    }

    @UiThread
    @Override
    public void responseError(String error) {

    }
}
